﻿<%@ Page CodeFileBaseClass="AliCMS.Lib.SystemPageBase" %>
<ali:Get ID="ThisChannel" Object="Channel" InnerID="Request.QueryString['ID']" Validate="True" />
<ali:List ID="ChannelTable" Object="Channel" />
<ali:List ID="ExtraInstanceTable" Object="Channel.ExtraInstance" Condition="ChannelID=ThisChannel[ID]" />
<ali:List ID="ModuleTable" Object="Module" />
<script language="c#" runat="server">
private DataRow _channelRow;
private string _languageAb;
private string _templatePath;
protected override void OnLoad(EventArgs e)
{
	base.OnLoad(e);
	
	if (Convert.IsDBNull(ThisChannel[Alias.Channel.TemplatePath]))
		throw new Exception("没有邦定模板！");
	
	_channelRow = Manager.Channels.Select(Alias.Channel.ID + "='" + ThisChannel[Alias.Channel.ID] + "'")[0];
	_languageAb = _channelRow[Alias.Channel.Lang].ToString();
	_templatePath = _channelRow[Alias.Channel.TemplatePath].ToString();
}

protected string GetModuleName(DataRow record)
{
	DataRow[] rows = ModuleTable.Select(Alias.Module.Namespace + "='" + record[Alias.Template.Module.ModuleID] + "'");
	if( rows.Length != 0)
		return rows[0][Alias.Module.Label].ToString();
	else
		return String.Empty;
}

Guid _selectedInstanceID = Guid.Empty;
Guid _selectedChannelID = Guid.Empty;
protected StringBuilder WriteBindingInstanceOptions(DataRow record)
{
	DataRow[] rows = ExtraInstanceTable.Select(Alias.Channel.ExtraInstance.Mark + "='" + record[Alias.Template.Module.Mark] + "'");
	if (rows.Length != 0)
	{
		_selectedInstanceID = (Guid)rows[0][Alias.Channel.ExtraInstance.BindingInstanceID];
		_selectedChannelID = (Guid)rows[0][Alias.Channel.ExtraInstance.BindingChannelID];
		if (_selectedInstanceID == Guid.Empty)
			_selectedInstanceID = (Guid)Manager.Channels.Select(Alias.Channel.ID + "='" + _selectedChannelID + "'")[0][Alias.Instance.ID];
	}
	else
	{
		_selectedInstanceID = Guid.Empty;
		_selectedChannelID = Guid.Empty;
	}

	StringBuilder result = new StringBuilder();	
	rows = Manager.Instances.Select(Alias.Module.Namespace + "='" + record[Alias.Template.Module.ModuleID] + "'");
	foreach (DataRow row in rows)
	{
		if (Guid.Equals(row[Alias.Instance.ID], _selectedInstanceID))
			result.Append("<option value=\"" + row[Alias.Instance.ID] + "\" selected=\"selected\">" + row[Alias.Instance.Name + PageLang] + "</option>");
		else
			result.Append("<option value=\"" + row[Alias.Instance.ID] + "\">" + row[Alias.Instance.Name + PageLang] + "</option>");
	}
	
	return result;
}

protected StringBuilder WriteBindingChannelOptions(DataRow record)
{
	StringBuilder result = new StringBuilder();	
	DataRow[] rows = ChannelTable.Select(Alias.Channel.InstanceID + "='" + _selectedInstanceID + "' AND " + Alias.Channel.Lang + "='" + ThisChannel[Alias.Channel.Lang] + "'");
	foreach (DataRow r in rows)
	{
		string name = r[Alias.Channel.Name].ToString();
		DataRow parent = r;
		while (!Guid.Equals(parent[Alias.Channel.ParentID], Guid.Empty))
		{
			parent = ChannelTable.Select(Alias.Channel.ID + "='" + parent[Alias.Channel.ParentID] + "'")[0];
			name = parent[Alias.Channel.Name] + " → " + name;
		}

		if (Guid.Equals(r[Alias.Channel.ID], _selectedChannelID))
			result.Append("<option value=\"" + r[Alias.Channel.ID] + "\" selected=\"selected\">" + name + "</option>");
		else
			result.Append("<option value=\"" + r[Alias.Channel.ID] + "\">" + name + "</option>");
	}
	
	return result;
}

protected string GetExtraInstanceInnerID(DataRow record)
{
	DataRow[] rows = ExtraInstanceTable.Select(Alias.Channel.ExtraInstance.Mark + "='" + record[Alias.Template.Module.Mark] + "'");
	if (rows.Length != 0)
		return rows[0][Alias.Channel.ExtraInstance.InnerID].ToString();
	else
		return String.Empty;
}
</script>
<section data-page="channel_extra_instance" data-title="栏目绑定" data-lang="<%=ContentLang%>" data-channel-lang="<%=ThisChannel[Lang]%>" data-channel-id="<%=ThisChannel[ID]%>">
	<dl class="path1">
		<dt><span>您的位置：</span><a href="list.aspx?Lang=<%=ContentLang%>" target="ali-navtab">栏目列表</a> → <b class="<%=ContentLang%>Font"><%=ThisChannel[Name]%></b></dt>
	</dl>
	<div class="post-body">
		<div class="ali-tabs" data-event="click">
			<div class="ali-tabs-header">
				<div class="ali-tabs-nav-container">
					<ul>
						<li><a href="edit.aspx?Lang=<%=ContentLang%>&amp;ID=<%=Request.QueryString["ID"]%>" target="ali-navtab">编辑</a></li>
						<li><a href="change_template.aspx?Lang=<%=ContentLang%>&amp;ID=<%=Request.QueryString["ID"]%>" target="ali-navtab">更改模板</a></li>
						<li class="ali-active"><a>栏目绑定</a></li>
						<%If Convert.ToBoolean(ThisChannel[EnableMenu])%>
							<li><a href="menu/list.aspx?Lang=<%=ContentLang%>&amp;ChannelID=<%=Request.QueryString["ID"]%>" target="ali-navtab">栏目菜单</a></li>
							<li><a href="menu/add.aspx?Lang=<%=ContentLang%>&amp;ChannelID=<%=Request.QueryString["ID"]%>" target="ali-navtab">添加菜单</a></li>
						<%EndIf%>
					</ul>
				</div>
			</div>
			<div class="ali-tabs-body">
				<div class="ali-tabs-panel"></div>
				<div class="ali-tabs-panel"></div>
				<div class="ali-tabs-panel">
					<table data-ali-widget="grid" class="grid1">
						<thead>
							<tr>
								<th>模块</th>
								<th>标志</th>
								<th>邦定的栏目</th>
								<th>设置</th>
							</tr>
						</thead>
						<tbody>
							<ali:List Object="Template.Module" Condition="TemplatePath=_templatePath">
								<ali:Record>
									<%If Convert.ToString(Mark) != ""%>
										<tr data-id="<%=GetExtraInstanceInnerID(Record)%>" data-mark="<%=Mark%>">
											<td><%=GetModuleName(Record)%></td>
											<td><%=Mark%></td>
											<td>
												<select name="BindingInstanceID" class="<%=ContentLang%>Font">
													<option value="">--</option>
													<%=WriteBindingInstanceOptions(Record)%>
												</select>
												<%If !Convert.ToBoolean(JustData)%>
													<select name="BindingChannelID" class="<%=ContentLang%>Font">
														<option value="">--</option>
														<%=WriteBindingChannelOptions(Record)%>
													</select>
												<%EndIf%>
											</td>
											<td>
												<button class="ali-button" data-operation="SetExtraInstance">设置</button>
											</td>
										</tr>
									<%EndIf%>
								</ali:Record>
							</ali:List>
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
</section>